import { reactive } from "./reactive"
import { tracker, trigger } from "./effect"

const isObject = (value) => {
  return value != null && typeof value === 'object'
}

/**
 * 其实ref碰到引用类型 也是调用reactive
 * @param value 
 * @returns 
 */
const toReactive = (value) => {
  return isObject(value) ? reactive(value) : value
}

/**
 * ref 支持普通类型和引用类型
 * @param value 
 */
export const ref = <T>(value: T) => {
  return new RefImpl<T>(value)
}

class  RefImpl<T> {

  private _value: T

  constructor(value) {
    this._value = toReactive(value)
  }

  get value(): T {
    tracker(this, 'value')
    return this._value
  }
  set value(newValue) {
    if(newValue == this._value) return
    this._value = toReactive(newValue)
    trigger(this, 'value')
  }
}